/**
 * Created by lwy on 2017/8/1.
 */


var page = 1;
var id;
var userAccount,
    realName,
    userTypeValue,
    userStateValue=1;
function doAjax(){
    $.ajax({
        url:'/crm/userC/getCRMUser.json',
        dataType:'json',
        data:{
            token:localStorage.getItem('crmtoken'),
            page:page,
            userAccount:userAccount,
            userStateValue:userStateValue,
            userTypeValue:userTypeValue,
            realName:realName,
        },
        type:'post',
        success: function (data) {
            if(data.code==-1){
                window.parent.location.href="/crm/login.jsp"
            }
            if(data.code==0){
                alert(data.msg);
            }
            if(data.code==1){
                $('#record').html(data.recordsTotal);
                $('#withdraw-record').empty();
                for(var i=0;i<data.datas.length;i++){
                    var del = data.datas[i].userTypeValue;
                    var delHtml = '';
                    var alterHtml = '';
                    var delStyle = '';
                    if(del==4||del==15){
                        if(data.datas[i].stateValue==1){
                            delHtml='无效';
                            alterHtml='修改';
                        }else if(data.datas[i].stateValue==4){
                            delHtml='有效';
                            alterHtml='';
                        }
                        delStyle='';
                    }else{
                        delHtml='';
                        alterHtml='';
                        delStyle='pointer-events: none;'
                    }
                    $('<tr class="gradeA odd">'+
                        '<a href=""><td class="sorting_1 withdrawId" style="text-decoration: underline;cursor: pointer;" id="'+data.datas[i].id+'">'+data.datas[i].id+'</td></a>'+
                        '<td class=" ">'+data.datas[i].account+'</td>'+
                        '<td class=" ">'+data.datas[i].realNam+'</td>'+
                        '<td class="center ">'+data.datas[i].phone+'</td>'+
                        '<td class="sorting_1">'+data.datas[i].userType+'</td>'+
                        '<td class="sorting_1">'+data.datas[i].role+'</td>'+
                        '<td class="sorting_1">'+data.datas[i].createTime+'</td>'+
                        '<td class="sorting_1">'+data.datas[i].state+'</td>'+
                        '<td class="sorting_1 delId" style="text-decoration: underline;cursor: pointer;'+delStyle+'" delid="'+data.datas[i].id+'">'+delHtml+'</td>'+
                        '<td class="sorting_1 alterId" style="text-decoration: underline;cursor: pointer;'+delStyle+'" id="'+data.datas[i].id+'"  data-toggle="modal" data-target="#myModal4">'+alterHtml+'</td>'+
                        '<td class="sorting_1 initialize" style="text-decoration: underline;cursor: pointer;" initialize="'+data.datas[i].id+'">初始化</td>'+
                        '</tr>').appendTo($('#withdraw-record'));
                }

                //id处理
                //用户权限树
                $('.withdrawId').click(function(){
                    id = $(this).attr('id');
                    localStorage.removeItem('crmRoleId');
                    localStorage.removeItem('crmTree');
                    localStorage.setItem('crmUserId',id);
                    window.open('/crm/userRolePrivilegeC/tree')

                })

                //初始化密码
                $('.initialize').unbind('click').click(function(){
                    $.ajax({
                        url:'/crm/userC/initCRMPassword.json',
                        dataType:'json',
                        data:{
                            id:$(this).attr('initialize'),
                            token:localStorage.getItem('crmtoken'),
                        },
                        type:'post',
                        success:function(data){
                            if(data.code==-1){
                                window.parent.location.href="/crm/login.jsp"
                            }
                            if(data.code==0){
                                alert(data.msg)
                                return;
                            }
                            if(data.code==1){
                                alert('初始化成功');
                            }

                        }
                    })
                })

                //无效/有效角色
                $('.delId').unbind('click').click(function(){
                    var recoverHtml = $(this).html();
                    var url = '';
                    if(recoverHtml=='无效'){
                        url='/crm/userC/invalidUser.json';
                    }else if(recoverHtml=='有效'){
                        url='/crm/userC/normalUser.json';
                    }
                    if(confirm("你确定要"+recoverHtml+"该用户？")){

                        $.ajax({
                            url:url,
                            dataType:'json',
                            data:{
                                token:localStorage.getItem('crmtoken'),
                                id:$(this).attr('delid'),
                            },
                            type:'post',
                            success:function(data){
                                if(data.code==-1){
                                    window.parent.location.href="/crm/login.jsp"
                                }
                                if(data.code==0){
                                    alert(data.msg)
                                    return;
                                }
                                if(data.code==1){
                                    alert(''+recoverHtml+'成功');
                                    page=1;
                                    doAjax();
                                }
                            }
                        })

                    }else{

                    }
                })

                //修改
                $('.alterId').unbind('click').click(function(){
                    id = $(this).attr('id');
                })
                $('#save2').unbind('click').click(function(){
                    $.ajax({
                        url:'/crm/userC/updateUserRole.json',
                        dataType:'json',
                        data:{
                            token:localStorage.getItem('crmtoken'),
                            id:id,
                            roleId:$('#chooselevel2').val(),
                        },
                        type:'post',
                        success:function(data){
                            if(data.code==-1){
                                window.parent.location.href="/crm/login.jsp"
                            }
                            if(data.code==0){
                                alert(data.msg)
                                return;
                            }
                            if(data.code==1){
                                alert('修改成功');
                                $('#myModal4').removeClass('fade in');
                                $('#myModal4').addClass('fade');
                                $('#myModal4').attr('aria-hidden',true);
                                $('#myModal4').css('display','none');
                                $('.modal-backdrop').css('display','none');
                                page=1;
                                doAjax();
                            }
                        }
                    })
                })

                $(".pagination").createPage({
                    totalPage:data.totalPage,
                    currPage:data.pageNow,
                    pageCount:data.pageNow,
                    turndown:'true',
                    backFn:function(p){
                        page=p;
                        doAjax();
                    }
                });
            }
        }
    })
}




$(function(){
    doAjax();
    doRole();
})

//查询
$('#refer').click(function(){
    userAccount = $('#accountName').val();
    realName = $('#name').val();
    userStateValue = $('#userState').val();
    userTypeValue = $('#userTypeValue').val(),
    page=1;
    doAjax();
})

//获取用户角色
function doRole(){
    $.ajax({
        url:'/crm/roleC/getInitValidRole.json',
        dataType:'json',
        data:{

        },
        type:'post',
        success:function(data){
            if(data.code==1){
                for(var i=0;i<data.datas.length;i++){
                    $('<option value="'+data.datas[i].id+'">'+data.datas[i].name+'</option>').appendTo($('#chooselevel'));
                    $('<option value="'+data.datas[i].id+'">'+data.datas[i].name+'</option>').appendTo($('#chooselevel2'));
                }
            }
        }
    })
}


//点击保存
$('#save').click(function(){

    var agencyloginName = $('#userAccount').val();
    var agencyphone = $('#userPhone').val();
    if(!checkUser(agencyloginName)){
        return;
    }
    if(!checkMobile(agencyphone)){
        return;
    }

    $.ajax({
        url:'/crm/userC/addUser.json',
        dataType:'json',
        data:{
            token:localStorage.getItem('crmtoken'),
            userAccount:agencyloginName,
            realName:$('#userName').val(),
            roleId:$('#chooselevel').val(),
            phone:agencyphone,
        },
        type:'post',
        success:function(data){
            if(data.code==0){
                alert(data.msg)
                return;
            }
            if(data.code==-1){
                window.parent.location.href="/crm/login.jsp"
            }
            if(data.code==1){
                alert('添加成功');
                $('#myModal5').removeClass('fade in');
                $('#myModal5').addClass('fade');
                $('#myModal5').attr('aria-hidden',true);
                $('#myModal5').css('display','none');
                $('.modal-backdrop').css('display','none');
                $('#myModal5 input').val('');
                page=1;
                doAjax();
            }
        }
    })
})

